//
//  CostqueryViewController.m
//  HarmonizePub
//
//  Created by apple on 2016/12/2.
//  Copyright © 2016年 All rights reserved.
//

#import "CostqueryViewController.h"
#import "UIBarButtonItem+Zy.h"
#import "CostdetailViewController.h"
#import "HTTPSTool.h"
#import "URLTool.h"
#import "Config.h"
#import <MJRefresh.h>
@interface CostqueryViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate>
{
    UITableView * mytabbleview;
    NSMutableArray * allarray;
    
    
    NSInteger selectinteger;
    BOOL issearch;
    NSString * selectstr;
}
@property (strong, nonatomic) UISearchController *searchController;
@end

@implementation CostqueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    selectinteger = 1;
    allarray = [[NSMutableArray alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.tableView setTableHeaderView:[self searchController].searchBar];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        selectinteger ++;
        if (issearch) {
            [self selectformationandpage:10 andcurpage:selectinteger andname:selectstr];
        }else
        {
         [self getinformationandpage:10 andcurpage:selectinteger];
        }
       
    }];
    UIBarButtonItem *leftItem = [UIBarButtonItem barButtonItemWithIcon:@"Back.png" highlightedIcon:nil target:self action:@selector(back)];
    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    nagetiveSpacer.width = -15;
    self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
    [self getinformationandpage:10 andcurpage:selectinteger];
    // Do any additional setup after loading the view from its nib.
}
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
    _searchController = nil;
//    self.tableView = nil;
}
//-(void)loadMoreData
//{
//    selectinteger ++;
//    [self getinformationandpage:10 andcurpage:selectinteger];
//}
-(void)getinformationandpage:(NSInteger)pagesize andcurpage:(NSInteger)curpage
{
      NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary * param = @{
                             @"pageSize":[NSString stringWithFormat:@"%ld",pagesize],
                             @"curPage":[NSString stringWithFormat:@"%ld",curpage]
                             };
 [HTTPSTool POSTRequestWithPath:XHgetCostList cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//     XHLog(@"费用项目 -> JSON: %@", JSON);
//     [allarray removeAllObjects];
     NSArray * arrays = [[NSArray alloc] initWithArray:JSON[@"data"][@"list"]];
     for (int i = 0; i< arrays.count; i++) {
         [allarray addObject:[arrays objectAtIndex:i]];
     }
     [self.tableView.mj_footer endRefreshing];
     [self.tableView reloadData];
 }];
}
#pragma mark 搜索调用接口
-(void)selectformationandpage:(NSInteger)pagesize andcurpage:(NSInteger)curpage andname:(NSString *)name;
{
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary * param = @{
                             @"pageSize":[NSString stringWithFormat:@"%ld",pagesize],
                             @"curPage":[NSString stringWithFormat:@"%ld",curpage],
                             @"name":name
                             };
    [HTTPSTool POSTRequestWithPath:XHgetCostList cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"%@",JSON);
        if (selectinteger == 1) {
            [allarray removeAllObjects];
        }
        NSArray * arrays = [[NSArray alloc] initWithArray:JSON[@"data"][@"list"]];
        for (int i = 0; i< arrays.count; i++) {
            [allarray addObject:[arrays objectAtIndex:i]];
        }
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    }];
}
#pragma tableview 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allarray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * inderstr = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:inderstr];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:inderstr];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.text = [allarray objectAtIndex:indexPath.row][@"fymc"];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CostdetailViewController * costdetailview = [[CostdetailViewController alloc] init];
    costdetailview.title = @"费用详情";
    costdetailview.datadic = [allarray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:costdetailview animated:YES];
}
#pragma mark 搜索框功能
- (UISearchController *)searchController {
    if (!_searchController) {
        _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
        _searchController.searchResultsUpdater = self;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchController.hidesNavigationBarDuringPresentation = YES;
        _searchController.searchBar.placeholder = @"搜索";
        [_searchController.searchBar sizeToFit];
        _searchController.searchBar.delegate = self;
        }
    issearch = NO;
//    selectinteger = 1;
    return _searchController;
}
#pragma mark - UISearchDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
        issearch = YES;
        selectinteger = 1;

    NSString * filterstring = searchController.searchBar.text;
    selectstr = filterstring;
    if ([filterstring length] >=2) {
        [self selectformationandpage:10 andcurpage:selectinteger andname:filterstring];
    }
    
    
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    issearch = NO;
    selectinteger = 1;
    
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSString * filterstring = searchBar.text;
  [self selectformationandpage:10 andcurpage:selectinteger andname:filterstring];
}
-(void)viewDidDisappear:(BOOL)animated
{
    if (_searchController.active) {
        _searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
